package jnpf.controller;

import cn.hutool.core.date.DateUtil;
import jnpf.service.ZXGL_OrganizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.service.VisualdevService;
import jnpf.base.util.VisualUtils;
import jnpf.exception.DataException;

import jnpf.model.common.ZXGL_Leaders;
import jnpf.service.ZXGL_XiangMuLiXiang_MingXiService;
import org.springframework.transaction.annotation.Transactional;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.engine.service.FlowTaskService;
import jnpf.entity.ZXGL_DuBanXiangMuEntity;
import jnpf.entity.ZXGL_XiangMuLiXiang_MingXiEntity;
import jnpf.exception.WorkFlowException;
import jnpf.generater.util.GeneraterSwapUtil;
import jnpf.model.zxgl_dubanxiangmu.*;
import jnpf.model.zxgl_dubanxiangmu.ZXGL_DuBanXiangMuPagination;
import jnpf.service.ZXGL_DuBanXiangMuService;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

import java.util.Date;
import java.util.List;


/**
 * ZXGL_DuBanXiangMu
 *
 * @版本： V2.0
 * @版权： 河北交投智能交通技术有限责任公司
 * @作者： 数字交投项目组
 * @日期： 2025-03-22
 */
@Slf4j
@RestController
@Api(tags = "项目督办", value = "zxgl")
@RequestMapping("/ZXGL_DuBanXiangMu")
public class ZXGL_DuBanXiangMuController {


    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private VisualdevService visualdevService;

    @Autowired
    private FlowTaskService flowTaskService;

    @Autowired
    private ZXGL_DuBanXiangMuService zXGL_DuBanXiangMuService;

    @Autowired
    private ZXGL_XiangMuLiXiang_MingXiService zxglXiangMuLiXiangMingXiService;

    @Autowired
    private ZXGL_OrganizeService zxglOrganizeService;


    /**
     * 列表
     *
     * @param zXGL_DuBanXiangMuPagination
     * @return
     */
    @ApiOperation("项目督办分页查询")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZXGL_DuBanXiangMuPagination zXGL_DuBanXiangMuPagination) throws IOException {
        List<ZXGL_DuBanXiangMuEntity> list = zXGL_DuBanXiangMuService.getList(zXGL_DuBanXiangMuPagination);
        //处理id字段转名称，若无需转或者为空可删除
        for (ZXGL_DuBanXiangMuEntity entity : list) {
            entity.setFqr(generaterSwapUtil.userSelectValues(entity.getFqr()));
            entity.setZzr(generaterSwapUtil.userSelectValues(entity.getZzr()));
        }
        List<ZXGL_DuBanXiangMuListVO> listVO = JsonUtil.getJsonToList(list, ZXGL_DuBanXiangMuListVO.class);
        for (ZXGL_DuBanXiangMuListVO zXGL_DuBanXiangMuVO : listVO) {
        }


        for (ZXGL_DuBanXiangMuListVO vo : listVO) {
            FlowTaskEntity flowTaskEntity = flowTaskService.getInfoSubmit(vo.getId());
            if (flowTaskEntity != null) {
                vo.setFlowState(flowTaskEntity.getStatus());
            } else {
                vo.setFlowState(null);
            }
        }
        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zXGL_DuBanXiangMuPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }


    /**
     * 创建
     *
     * @param zXGL_DuBanXiangMuCrForm
     * @return
     */
    @ApiOperation("保存项目督办信息")
    @PostMapping
    @Transactional
    public ActionResult create(@RequestBody @Valid ZXGL_DuBanXiangMuCrForm zXGL_DuBanXiangMuCrForm) throws DataException {
        String visualId = "673056671173473797";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        String mainId = RandomUtil.uuId();
        UserInfo userInfo = userProvider.get();

        // reggion 赋值部门领导和负责人
        ZXGL_Leaders leaders = zxglOrganizeService.getLeaders();
        zXGL_DuBanXiangMuCrForm.setDepartmentManager(leaders.getDepartmentManager());
        zXGL_DuBanXiangMuCrForm.setDepartmentHeadleader(leaders.getDepartmentHeadleader());

        ZXGL_XiangMuLiXiang_MingXiEntity model = zxglXiangMuLiXiangMingXiService.getInfo(zXGL_DuBanXiangMuCrForm.getXmid());
        zXGL_DuBanXiangMuCrForm.setDepartmentManagerZzbm(model.getDepartmentManagerZzbm());
        zXGL_DuBanXiangMuCrForm.setDepartmentHeadleaderZzbm(model.getDepartmentHeadleaderZzbm());
        zXGL_DuBanXiangMuCrForm.setZzrbmfzr(model.getDepartmentManagerZzbm());
        // endreion

        ZXGL_DuBanXiangMuEntity entity = JsonUtil.getJsonToBean(zXGL_DuBanXiangMuCrForm, ZXGL_DuBanXiangMuEntity.class);
        entity.setId(mainId);
        entity.setCreatorTime(new Date());



        zXGL_DuBanXiangMuService.save(entity);
        if (FlowStatusEnum.submit.getMessage().equals(zXGL_DuBanXiangMuCrForm.getStatus())) {
            try {
                VisualUtils.submitFlowTask(visualdevEntity, mainId, zXGL_DuBanXiangMuCrForm, userInfo, zXGL_DuBanXiangMuCrForm.getCandidateList());
                FlowTaskEntity flowTaskEntity = flowTaskService.getInfo(mainId);
                if (flowTaskEntity != null) {
                    flowTaskEntity.setTitle(zXGL_DuBanXiangMuCrForm.getXmmc() + "-项目督办-" + userInfo.getUserName() + "-" + DateUtil.today());
                    flowTaskEntity.setDataTable("zxgl_dubanxiangmu");
                    flowTaskService.update(flowTaskEntity);
                }
            } catch (WorkFlowException e) {
                e.printStackTrace();
            }
            return ActionResult.success("提交成功，请耐心等待");
        }
        return ActionResult.success("保存成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("获取项目督办信息")
    @GetMapping("/{id}")
    public ActionResult<ZXGL_DuBanXiangMuInfoVO> info(@PathVariable("id") String id) {
        ZXGL_DuBanXiangMuEntity entity = zXGL_DuBanXiangMuService.getInfo(id);
        ZXGL_DuBanXiangMuInfoVO vo = JsonUtil.getJsonToBean(entity, ZXGL_DuBanXiangMuInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @return
     */
    @ApiOperation("修改项目督办信息")
    @PutMapping("/{id}")
    @Transactional
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZXGL_DuBanXiangMuUpForm zXGL_DuBanXiangMuUpForm) throws DataException {
        String visualId = "673056671173473797";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        UserInfo userInfo = userProvider.get();
        ZXGL_DuBanXiangMuEntity entity = zXGL_DuBanXiangMuService.getInfo(id);
        if (entity != null) {

            // reggion 赋值部门领导和负责人
            ZXGL_Leaders leaders = zxglOrganizeService.getLeaders();
            zXGL_DuBanXiangMuUpForm.setDepartmentManager(leaders.getDepartmentManager());
            zXGL_DuBanXiangMuUpForm.setDepartmentHeadleader(leaders.getDepartmentHeadleader());

            ZXGL_XiangMuLiXiang_MingXiEntity model = zxglXiangMuLiXiangMingXiService.getInfo(zXGL_DuBanXiangMuUpForm.getXmid());
            zXGL_DuBanXiangMuUpForm.setDepartmentManagerZzbm(model.getDepartmentManagerZzbm());
            zXGL_DuBanXiangMuUpForm.setDepartmentHeadleaderZzbm(model.getDepartmentHeadleaderZzbm());
            zXGL_DuBanXiangMuUpForm.setZzrbmfzr(model.getDepartmentManagerZzbm());
            // endreion

            ZXGL_DuBanXiangMuEntity subentity = JsonUtil.getJsonToBean(zXGL_DuBanXiangMuUpForm, ZXGL_DuBanXiangMuEntity.class);



            zXGL_DuBanXiangMuService.update(id, subentity);

            if (FlowStatusEnum.submit.getMessage().equals(zXGL_DuBanXiangMuUpForm.getStatus())) {
                try {
                    VisualUtils.submitFlowTask(visualdevEntity, id, subentity, userInfo, zXGL_DuBanXiangMuUpForm.getCandidateList());
                    FlowTaskEntity flowTaskEntity = flowTaskService.getInfo(id);
                    if (flowTaskEntity != null) {
                        flowTaskEntity.setTitle(zXGL_DuBanXiangMuUpForm.getXmmc() + "-项目督办-" + userInfo.getUserName() + "-" + DateUtil.today());
                        flowTaskEntity.setDataTable("zxgl_dubanxiangmu");
                        flowTaskService.update(flowTaskEntity);
                    }
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
                return ActionResult.success("提交成功，请耐心等待");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除项目督办信息")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZXGL_DuBanXiangMuEntity entity = zXGL_DuBanXiangMuService.getInfo(id);
        if (entity != null) {
            FlowTaskEntity taskEntity = flowTaskService.getInfoSubmit(id);
            if (taskEntity != null) {
                try {
                    flowTaskService.delete(taskEntity);
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
            }
            zXGL_DuBanXiangMuService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }

    /**
     * 根据项目获取督办记录
     *
     * @param xmId
     * @return
     * @throws IOException
     */
    @ApiOperation("根据项目获取督办记录")
    @GetMapping("/getListByXmId/{xmId}")
    public ActionResult getListByXmId(@PathVariable String xmId) throws IOException {
        return ActionResult.success(zXGL_DuBanXiangMuService.getList(xmId));
    }
}
